import EventId = zs.laya.game.EventId;
import D from "../test/Data"
//import HA from "../test/HandAnimation"

export default class GamePlayUI extends Laya.Script {

    /** @prop {name:goldLabel,type:Node}*/
    public goldLabel: Laya.Label;
    /** @prop {name:otherLabel,type:Node}*/
    public otherLabel: Laya.Label;
    /** @prop {name:scoreLabel,type:Node}*/
    public scoreLabel: Laya.Label;

    private fullUI: Laya.Image;

    private middleUI: Laya.Box;
    private award: Laya.Image;
    private image: Laya.Image;
    private title: Laya.Image;
    private guideTips: Laya.Image;

    private bottomUI: Laya.Box;
    private nextBtn: Laya.Image;
    private videoBtn: Laya.Image;
    private startBtn: Laya.Image;
    private fixBtn: Laya.Image;
    private guideHand: Laya.Image;
    private nextStep: Laya.Image;


    private LoseArr: Array<Laya.Image> = new Array<Laya.Image>();
    private GuiArr: Array<Laya.Image> = new Array<Laya.Image>();
    private WinArr: Array<Laya.Image> = new Array<Laya.Image>();
    private NextStepArr: Array<Laya.Image> = new Array<Laya.Image>();

    constructor() { super(); }

    onEnable(): void {
        super.onEnable();
        //this.goldLabel.text = zs.laya.game.AppMain.playerInfo.gold.toString();
        Laya.stage.event(EventId.GAME_START);
        // Laya.stage.on(EventId.EGG_GET_AWARD,this,this.onGetEggReward);
        this.InitData();
        this.InitEvent();
    }

    onDisable(): void {
        super.onDisable();
    }

    onClick() {
        // zs.laya.game.UIService.showKnockEggView();
        // let random:number = Math.round(Math.random()*10);
        // if(random < 5){
        //     Laya.stage.event(EventId.GAME_FAILED);
        // }else{
        //     Laya.stage.event(EventId.GAME_WIN);
        // }      
        // console.log("响应事件");
        //Laya.stage.event(D.Lose_Event);  
    }

    private InitData() {
        this.fullUI = this.owner.getChildByName("fullUI") as Laya.Image;

        this.middleUI = this.owner.getChildByName("middleUI") as Laya.Box;
        this.award = this.middleUI.getChildByName("award") as Laya.Image;
        this.title = this.middleUI.getChildByName("title") as Laya.Image;
        this.image = this.middleUI.getChildByName("image") as Laya.Image;
        this.guideTips = this.middleUI.getChildByName("guideTips") as Laya.Image;

        this.bottomUI = this.owner.getChildByName("bottomUI") as Laya.Box;
        this.nextBtn = this.bottomUI.getChildByName("nextBtn") as Laya.Image;
        this.videoBtn = this.bottomUI.getChildByName("videoBtn") as Laya.Image;
        this.startBtn = this.bottomUI.getChildByName("startBtn") as Laya.Image;
        this.fixBtn = this.bottomUI.getChildByName("fixBtn") as Laya.Image;
        this.nextStep = this.bottomUI.getChildByName("nextStep") as Laya.Image;
        this.guideHand = this.bottomUI.getChildByName("guideHand") as Laya.Image;

        this.LoseArr.push(this.fullUI);
        this.LoseArr.push(this.image);
        this.LoseArr.push(this.title);

        this.GuiArr.push(this.guideTips);
        this.GuiArr.push(this.fixBtn);
        this.GuiArr.push(this.guideHand);

        this.WinArr.push(this.fullUI);
        this.WinArr.push(this.award);
        this.WinArr.push(this.videoBtn);
        this.WinArr.push(this.nextBtn);

        this.NextStepArr.push(this.nextStep);

        Laya.timer.loop(1000, this, this.Tween);
    }

    private Tween() {
        Laya.Tween.to(this.guideHand, { scaleX: 0.7, scaleY: 0.7 }, 500, null, Laya.Handler.create(this, function () {
            Laya.Tween.to(this.guideHand, { scaleX: 1, scaleY: 1 }, 500);
        }));
    }

    private InitEvent() {
        Laya.stage.on(D.Lose_Event, this, this.Lose);
        Laya.stage.on(D.Win_Event, this, this.Win);
        Laya.stage.on(D.Gui_Event, this, this.Gui);
        Laya.stage.on(D.NextStep_Event, this, this.NextStep);
        Laya.stage.on(D.All_Event, this, this.All);
    }

    private Lose() {
        console.log("Lose");
        this.IsVisibleController("Lose");
    }

    private Gui() {
        console.log("Gui");
        this.IsVisibleController("Gui");
        this.fixBtn.on(Laya.Event.CLICK, this, this.OnFixBtn);
    }

    private Win() {
        console.log("Win");
        this.IsVisibleController("Win");
    }

    private NextStep() {
        console.log("NextStep");
        this.IsVisibleController("NextStep");
        this.nextStep.on(Laya.Event.CLICK, this, this.OnNextBtn);
    }

    private All() {
        console.log("All");
        this.IsVisibleController("All");
    }

    private OnNextBtn() {
        console.log("OnNextBtn_Game");
        Laya.stage.off(D.NextStep_Event, this, this.NextStep);
        Laya.stage.event(D.SceneNextStep_Event);
    }

    private OnFixBtn() {
        Laya.stage.event(D.SceneBlow_Event);
    }

    private IsVisibleController(str: string) {
        switch (str) {
            case "Lose":
                this.isVisable(this.LoseArr, true);
                this.isVisable(this.GuiArr, false);
                this.isVisable(this.WinArr, false);
                this.isVisable(this.NextStepArr, false);
                break;
            case "Gui":
                this.isVisable(this.LoseArr, false);
                this.isVisable(this.GuiArr, true);
                this.isVisable(this.WinArr, false);
                this.isVisable(this.NextStepArr, false);
                break;
            case "Win":
                this.isVisable(this.LoseArr, false);
                this.isVisable(this.GuiArr, false);
                this.isVisable(this.WinArr, true);
                this.isVisable(this.NextStepArr, false);
                break;
            case "NextStep":
                this.isVisable(this.LoseArr, false);
                this.isVisable(this.GuiArr, false);
                this.isVisable(this.WinArr, false);
                this.isVisable(this.NextStepArr, true);
                break;
            case "All":
                this.isVisable(this.LoseArr, false);
                this.isVisable(this.GuiArr, false);
                this.isVisable(this.WinArr, false);
                this.isVisable(this.NextStepArr, false);
                break;
        }
    }

    private isVisable(a: Array<Laya.Image>, isV: boolean) {
        for (let i = 0; i < a.length; i++) {
            a[i].visible = isV;
        }
    }

    // onGetEggReward(){
    //     let random:number = Math.round(Math.random()*10);
    //     if(random < 5){
    //         Laya.stage.event(EventId.GAME_FAILED);
    //     }else{
    //         Laya.stage.event(EventId.GAME_WIN);
    //     }
    // }
}